package renshi.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import renshi.base.BaseAction;
import renshi.dao.ApplyDAO;
import renshi.dao.TeacherDAO;
import renshi.model.Apply;
import renshi.model.Teacher;

public class TeacherAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private List<Teacher> list = new ArrayList<Teacher>();
	private List<Apply> list1 = new ArrayList<Apply>();
	private String loginname;
	private String turename;
	private int role;
	private String pwd;
	private int radiobutton;
	private int status;
	private String num;
	private int aid;
	private int days;	
	
	public List<Teacher> getList() {
		return list;
	}
	public void setList(List<Teacher> list) {
		this.list = list;
	}
	public List<Apply> getList1() {
		return list1;
	}
	public void setList1(List<Apply> list1) {
		this.list1 = list1;
	}
	public String getLoginname() {
		return loginname;
	}
	public void setLoginname(String loginname) {
		this.loginname = loginname;
	}
	public String getTurename() {
		return turename;
	}
	public void setTurename(String turename) {
		this.turename = turename;
	}
	public int getRole() {
		return role;
	}
	public void setRole(int role) {
		this.role = role;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public int getRadiobutton() {
		return radiobutton;
	}
	public void setRadiobutton(int radiobutton) {
		this.radiobutton = radiobutton;
	}
	public int getStatus() {
		return status;
	}
	public void setStatus(int status) {
		this.status = status;
	}
	public String getnum() {
		return num;
	}
	public void setNum(String num) {
		this.num = num;
	}
	public int getAid() {
		return aid;
	}
	public void setAid(int aid) {
		this.aid = aid;
	}
	public int getDays() {
		return days;
	}
	public void setDays(int days) {
		this.days = days;
	}
	//������Ϣ
	public String select()throws Exception{
	    HttpServletRequest request = ServletActionContext.getRequest();
		openCon();
		con.setAutoCommit(false);
		TeacherDAO teacherDAO=new TeacherDAO(con);
		List<Teacher> list=teacherDAO.queryall();
		con.commit();
		if(list!=null&&list.size()>0){
			request.setAttribute("list",list);
			return "selectSuccess";
		}else{
			return "queryFail";
		}
    }	
	//�޸ĸ�����Ϣ
	public String update()throws Exception{
		openCon();
		con.setAutoCommit(false);
		TeacherDAO teacherDAO = new TeacherDAO(con);
		boolean flag = teacherDAO.update(loginname,pwd);
		con.commit();
		if(flag==true){
			return "updateSuccess";
		}
		else{
			return "queryFail";
		}
	}
	//�������ٵ�
	public String applyquery() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		openCon();
		con.setAutoCommit(false);
		ApplyDAO applyDAO=new ApplyDAO(con);
		List<Apply> list0=applyDAO.query();
		con.commit();
		if(list0!=null&&list0.size()>0){
			request.setAttribute("list0",list0);
			return "applyquerySuc";
		}else{
			return "applyqueryFail";
		}
	}
	//��ʷ��ٵ�
	public String applyhistory() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		openCon();
		con.setAutoCommit(false);
		ApplyDAO applyDAO=new ApplyDAO(con);
		List<Apply> list2=applyDAO.history();
		con.commit();
		if(list2!=null&&list2.size()>0){
			request.setAttribute("list2",list2);
			return "applyhistorySuc";
		}else{
			return "queryFail";
		}
	}
	//�������ٵ���ϸ��Ϣ
	public String applycheck() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		openCon();
		con.setAutoCommit(false);
		ApplyDAO applyDAO=new ApplyDAO(con);
		List<Apply> list1=applyDAO.queryall(aid);
		con.commit();
		if(list1!=null&&list1.size()>0){
			request.setAttribute("list1",list1);
			return "applycheckSuc";
		}else{
			return "queryFail";
		}
	}
	//�������ٵ���ϸ��Ϣ
	public String applydetail() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		openCon();
		con.setAutoCommit(false);
		ApplyDAO applyDAO=new ApplyDAO(con);
		List<Apply> list1=applyDAO.queryall(aid);
		con.commit();
		if(list1!=null&&list1.size()>0){
			request.setAttribute("list1",list1);
			return "applydetailSuc";
		}else{
			return "queryFail";
		}
	}
	//���ѧ��
	public String countstu() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		openCon();
		con.setAutoCommit(false);
		ApplyDAO applyDAO=new ApplyDAO(con);
		List<Apply> list3=applyDAO.countstu();
		con.commit();
		if(list3!=null&&list3.size()>0){
			request.setAttribute("list3",list3);
			return "countstuSuc";
		}else{
			return "queryFail";
		}
	}
	//-----------��ѧ���ѯ��ٵ�
	public String countstu1() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		openCon();
		con.setAutoCommit(false);
		ApplyDAO applyDAO=new ApplyDAO(con);
		List<Apply> list3=applyDAO.countstu1();
		con.commit();
		if(list3!=null&&list3.size()>0){
			request.setAttribute("list3",list3);
			return "countstuSuc1";
		}else{
			return "queryFail";
		}
	}
	//ѧ��������
	public String applycount() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		openCon();
		con.setAutoCommit(false);
		ApplyDAO applyDAO=new ApplyDAO(con);
		List<Apply> list4=applyDAO.count(num);
		con.commit();
		if(list4!=null&&list4.size()>0){
			request.setAttribute("list4",list4);
			return "applycountSuc";
		}else{
			return "queryFail";
		}
	}
	//�����ٵ�
	public String applyupdate() throws Exception{
		if(radiobutton==0){
			int status=2;
			openCon();
			con.setAutoCommit(false);
			ApplyDAO applyDAO = new ApplyDAO(con);
			boolean flag = applyDAO.update(status,aid);
			con.commit();
			if(flag==true){
				return "applyupdateSuc";
			}
			else{
				return "queryFail";
			}
		}
		else if(radiobutton==1){
			int status=3;
			openCon();
			con.setAutoCommit(false);
			ApplyDAO applyDAO = new ApplyDAO(con);
			boolean flag = applyDAO.update(status,aid);
			con.commit();
			if(flag==true){
				return "applyupdateSuc";
			}
			else{
				return "queryFail";
			}
		}
		else
			return "queryFail";
	}
	//������û�
	public String add()throws Exception{
		openCon();
		con.setAutoCommit(false);
		TeacherDAO teacherDAO = new TeacherDAO(con);
		boolean flag = teacherDAO.add(loginname,turename,role,pwd);
		con.commit();
		if(flag==true){
			return "addSuccess";
		}
		else{
			return "addFail";
		}
	}
}
